package com.honeycomb.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 工具类
 * 
 * @author Wangyu
 */
public class SystemTools {
	
	//加密页面敏感信息
	public static String encrypt(String oriStr) {
		return Base64Utils.idHandle(oriStr, SystemConstants.PASSWORD_KEY, SystemConstants.ENCIPHERMENT);
	}
	
	//解密页面敏感信息
	public static String decrypt(String cypherStr) {
		return Base64Utils.idHandle(cypherStr, SystemConstants.PASSWORD_KEY, SystemConstants.DECIPHERING);
	}


	//转化数字型字符串
	public static final int string2Int(String str, int defaultVal) {
		try {
			return Integer.parseInt(str);
		} catch (Exception ex) {
		}
		return defaultVal;
	}
	
	/**
	 *  得到UUID
	 * @return String
	 * 				返回32位的UUID，去除了中间的连字号
	 * @author Zhongjiang
	 */
	public static final String getUUIDString() {
		return UUID.randomUUID().toString().replaceAll("-", "");
	}

	/**
	 *  拆分字符串
	 * @param str
	 * 				被拆分的字符串
	 * @param c
	 * 				分隔符
	 * @param max
	 * 				最多拆分数
	 * @return String[]
	 * 				返回拆分好的字符串，如果不符合要去，返回null
	 * @author Wangyu
	 */
	public static final String[] splitStr(String str, String c, int max) {
		if (SystemValidate.isEmpty(str)) {
			return null;
		}
		String[] strArr = str.split(c);
		for (int i = 0; i < strArr.length; i++) {
			if (SystemValidate.isEmpty(strArr[i])) {
				return null;
			}
		}
		if (strArr.length > max) {
			return null;
		}
		return strArr;
	}

	/**
	 *  拆分字符串
	 * @param str
	 * 				被拆分的字符串
	 * @param c
	 * 				分隔符
	 * @author Wangyu 
	 */
	public static final String[] splitStr(String str, String c) {
		return splitStr(str, c, 999999);
	}
	
	public static final String convertSpecialMark(String str) {
		str = str.replaceAll("<", "&lt");
		str = str.replaceAll(">", "&gt");
		str = str.replaceAll(" ", "&nbsp;");
		return str;
	}
	
	public static String removeImgTagFromHtml(String html) {
		Matcher imgCotentMatcher = Pattern.compile("<\\s*img\\s+([^>]*)>").matcher(html);
		return imgCotentMatcher.replaceAll(" ");
	}
	
	public static List<String> getImgTagFromHtml(String html) {
		Matcher imgCotentMatcher = Pattern.compile("<\\s*img\\s+([^>]*)>").matcher(html);
		List<String> imgUrlList = new ArrayList<String>();
		Pattern imgUrlPattern = Pattern.compile("src\\s*=\\s*\"([^\"]+)\"");
		while(imgCotentMatcher.find()) {
			Matcher imgUrlMatcher = imgUrlPattern.matcher(imgCotentMatcher.group());
	        if(imgUrlMatcher.find()){
	        	String srcAttr = imgUrlMatcher.group();
	        	imgUrlList.add(srcAttr.substring(srcAttr.indexOf('"')+1, srcAttr.lastIndexOf('"')));  
	        }  
		}	

		return imgUrlList;
	}
	
	public static List<Map<String, String>> getEmbedTagFromHtml(String html) {
		Matcher embedTagMatcher = Pattern.compile("<\\s*embed\\s+([^>]*)>").matcher(html);
		List<Map<String, String>> embedTagList = new ArrayList<Map<String, String>>();
		Pattern srcPattern = Pattern.compile("src\\s*=\\s*\"([^\"]+)\"");
		Pattern typePattern = Pattern.compile("type\\s*=\\s*\"([^\"]+)\"");
		
		while(embedTagMatcher.find()) {
			String embedTagHtml = embedTagMatcher.group();
			Map<String,String> embedTagStruct = new HashMap<String, String>(3);
			embedTagStruct.put("html", embedTagHtml);
			Matcher srcMatcher = srcPattern.matcher(embedTagHtml);
	        if(srcMatcher.find()){
	        	String srcAttr = srcMatcher.group();
	        	embedTagStruct.put("src", srcAttr.substring(srcAttr.indexOf('"')+1, srcAttr.lastIndexOf('"')));
	        }
			Matcher typeMatcher = typePattern.matcher(embedTagHtml);
	        if(typeMatcher.find()){
	        	String typeAttr = typeMatcher.group();
	        	embedTagStruct.put("type", typeAttr.substring(typeAttr.indexOf('"')+1, typeAttr.lastIndexOf('"')));
	        }
	        
	        embedTagList.add(embedTagStruct);
		}	

		return embedTagList;
	}
	
	public static String createShowTime(MyDateDTTM myDTTM){
		String normalDate = "";
		int nowDateInt = MyDateDTTM.convertCurrentDTTMtoInt();
		int difference = nowDateInt - myDTTM.value();
		/*刚刚（<5分钟）、5分钟前、15分钟前、40分钟前、x小时前（x取值是1~12）、x天前（x取值1~30）、x个月前（x取值1~12）、x年前*/
		if (difference >=0 && difference <= 5*60){
			normalDate = "刚刚";
		} else if (difference > 5*60 && difference <= 15*60){
			normalDate = "5分钟前";
		} else if (difference > 15*60 && difference <= 40*60){
			normalDate = "15分钟前";
		} else if (difference > 40*60 && difference <= 60*60){
			normalDate = "40分钟前";
		} else if (difference > 60*60 && difference <= 24*60*60) {
			//normalDate = String.valueOf(nowDateYMDHMS[3] - beforeDateYMDHMS[3]) +"小时前";
			normalDate = String.valueOf(difference / (3600)) +"小时前";
		} else if (difference > 24*60*60 && difference <= 30*24*60*60){
			//normalDate = String.valueOf(nowDateYMDHMS[3] - beforeDateYMDHMS[3]) + "天以前";
			normalDate = String.valueOf(difference / (3600 * 24)) +"天前";
		}  else if (difference > 30*24*60*60 && difference <= 12*30*24*60*60){
			normalDate = String.valueOf(difference / (3600 * 24 * 30)) +"月前";
		} else if (difference > 12*30*24*60*60){
			normalDate = String.valueOf(difference / (3600 * 24 * 30 * 12)) +"年前";
		}
		
		return normalDate;
	}
	
	public static void main(String[] args) {
		//得到当前时间的int值，包括年月日时分秒，存数据库用的
		System.out.println(MyDateDTTM.convertCurrentDTTMtoInt());
		
		//这个是把日期时间型的int转换为字符串，用于从数据库里面取出int值，显示在界面用的
		int dttmInt = MyDateDTTM.convertCurrentDTTMtoInt();
		String dttmStr = MyDateDTTM.formatDTTM(MyDateDTTM.convertIntToDTTM(dttmInt));
		System.out.println(dttmStr);
		
		//得到当前日期的int值，只有年月日，存数据库用的
		System.out.println(MyDateDTTM.convertCurrentDTtoInt());

		//这个是把日期型的int转换为字符串，用于从数据库里面取出int值，显示在界面用的
		int dtInt = MyDateDTTM.convertCurrentDTtoInt();
		String dtStr = MyDateDTTM.formatDT(MyDateDTTM.convertIntToDT(dtInt));
		System.out.println(dtStr);
		
		String ss = "2013-07-03";
		MyDateDTTM.convertDTtoInt(MyDateDTTM.formatStringToDateDT(ss));
	}
	
}
